package com.hc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hc.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    /**
     * 批量添加用户
     *
     * @param users
     * @return
     */
    int batchInsert(@Param("users") List<User> users);

    /**
     * 获取拥有指定role的用户
     * 应用场景：当Role修改（比如，修改role的code）时，拥有它的用户需要知道，从而清除Redis中的authority
     * @param roleId
     * @return
     */
    @Select("SELECT DISTINCT user_id " +
            "FROM tb_user_role " +
            "WHERE role_id = #{roleId}")
    List<Integer> selectUserIdsByRoleId(@Param("roleId") Integer roleId);

    /**
     * 获取拥有指定permission的用户
     * 应用场景：当permission修改时，拥有它的用户需要知道，从而清除Redis中的authority
     * @param PermissionId
     * @return
     * SQL语句的另一种写法：
     * SELECT DISTINCT user_id
     * FROM tb_user_role
     * WHERE tb_user_role.role_id IN
     * (
     * SELECT tb_role_permission.role_id
     * FROM tb_role_permission
     * WHERE tb_role_permission.permission_id =8
     * )
     */
    @Select("SELECT DISTINCT tb_user_role.user_id " +
            "FROM " +
            "	tb_role_permission " +
            "	INNER JOIN tb_role ON tb_role_permission.role_id = tb_role.id " +
            "	INNER JOIN tb_user_role ON tb_role.id = tb_user_role.role_id " +
            "WHERE	tb_role_permission.permission_id = #{permissionId} ;")
    List<Integer> selectUserIdsByPermissionId(@Param("permissionId") Integer PermissionId);
}
